package com.naihe.anderson.sms.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.adapter.BaseSetCheckAdapter;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.BaseSetCheckObject;
import com.naihe.anderson.sms.manager.NhSmsMgr;

public class BaseSetActivity extends Activity implements OnClickListener,OnItemClickListener {

	private static final int NOTIFICATION = 20110;
	private static final int CONFIRMPASSWORD = 20111;
	private static final int SETPASSWORD = 20112;
	NhSmsApp nhSmsApp;
	Button back_btn;
	NhSmsMgr nhSmsMgr;

	LinearLayout saveLayout;
	ListView saveList;
	BaseSetCheckAdapter saveCheckAdapter;
	List<BaseSetCheckObject> saveCheckObjects = new ArrayList<BaseSetCheckObject>();
	
	LinearLayout smsSetLayout;
	ListView smsSetList;
	BaseSetCheckAdapter smsSetCheckAdapter;
	List<BaseSetCheckObject> smsSetCheckObjects = new ArrayList<BaseSetCheckObject>();
	
	LinearLayout smsNotifLayout;
	ListView smsNotifList;
	BaseSetCheckAdapter smsNotifCheckAdapter;
	List<BaseSetCheckObject> smsNotifCheckObjects = new ArrayList<BaseSetCheckObject>();
	
	LinearLayout smsSafetyLayout;
	ListView smsSafetyList;
	BaseSetCheckAdapter smsSafetyCheckAdapter;
	List<BaseSetCheckObject> smsSafetyCheckObjects = new ArrayList<BaseSetCheckObject>();
	
	private LayoutParams changeLayoutParams(int size, LinearLayout layout){
		LayoutParams para = (LayoutParams) layout.getLayoutParams();
		para.height = size * 75 + 10;
		para.width = LayoutParams.FILL_PARENT;
		return para;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.base_set_layout);

		nhSmsApp = (NhSmsApp) getApplication();
		nhSmsApp.setActivity(this);
		nhSmsMgr = nhSmsApp.getNhSmsMgr();
		
		initView();
		initSaveView();
		initSmsSetView();
		initsmsNotifView();
		initSafetyView();
	}

	private void initView() {
		back_btn = (Button) findViewById(R.id.back_btn);
		back_btn.setOnClickListener(this);
	}
	
	private void initSafetyView(){
		smsSafetyLayout = (LinearLayout) findViewById(R.id.smsSafetyLayout);
		smsSafetyList = (ListView) findViewById(R.id.smsSafetyList);
		
		BaseSetCheckObject baseSetCheckObject = new BaseSetCheckObject();
		baseSetCheckObject.setTitle(getResources().getString(R.string.sms_safety_set_title));
		baseSetCheckObject.setExplain(getResources().getString(R.string.sms_safety_set_title_explain));
		baseSetCheckObject.setIfChcek(!TextUtils.isEmpty(nhSmsMgr.getBaseSafetyCipher()));
		baseSetCheckObject.setHeadSet(true);
		baseSetCheckObject.setHasCheck(true);
		smsSafetyCheckObjects.add(baseSetCheckObject);
		
		baseSetCheckObject = new BaseSetCheckObject();
		baseSetCheckObject.setTitle(getResources().getString(R.string.sms_safety_set_vibor_title));
		baseSetCheckObject.setExplain(getResources().getString(R.string.sms_safety_set_vibor_explain));
		baseSetCheckObject.setIfChcek(nhSmsMgr.getBaseCipherViewVibor());
		baseSetCheckObject.setHasCheck(true);
		smsSafetyCheckObjects.add(baseSetCheckObject);
		
		baseSetCheckObject = new BaseSetCheckObject();
		baseSetCheckObject.setTitle(getResources().getString(R.string.sms_safety_set_visible_title));
		baseSetCheckObject.setExplain(getResources().getString(R.string.sms_safety_set_visible_explain));
		baseSetCheckObject.setIfChcek(nhSmsMgr.getBaseCipherViewVisible());
		baseSetCheckObject.setHasCheck(true);
		smsSafetyCheckObjects.add(baseSetCheckObject);
		
		smsSafetyCheckAdapter = new BaseSetCheckAdapter(this, smsSafetyCheckObjects, !TextUtils.isEmpty(nhSmsMgr.getBaseSafetyCipher()));
		smsSafetyList.setAdapter(smsSafetyCheckAdapter);
		smsSafetyList.setOnItemClickListener(this);
		smsSafetyLayout.setLayoutParams(changeLayoutParams(smsSafetyCheckObjects.size(), smsSafetyLayout));
	}
	
	private void initSaveView(){
		saveLayout = (LinearLayout) findViewById(R.id.saveLayout);
		saveList = (ListView) findViewById(R.id.saveList);
		
		BaseSetCheckObject baseSetCheckObject = new BaseSetCheckObject();
		baseSetCheckObject.setTitle(getResources().getString(R.string.base_set_del_title_text));
		baseSetCheckObject.setExplain(getResources().getString(R.string.base_set_del_explain_text));
		baseSetCheckObject.setIfChcek(true);
		baseSetCheckObject.setHeadSet(true);
		baseSetCheckObject.setHasCheck(true);
		saveCheckObjects.add(baseSetCheckObject);
		
		baseSetCheckObject = new BaseSetCheckObject();
		baseSetCheckObject.setTitle(getResources().getString(R.string.del_sms_text));
		baseSetCheckObject.setExplain(getResources().getString(R.string.del_sms_set_text, 500));
		baseSetCheckObject.setIfChcek(false);
		baseSetCheckObject.setHasCheck(false);
		saveCheckObjects.add(baseSetCheckObject);
		
		saveCheckAdapter = new BaseSetCheckAdapter(this, saveCheckObjects, true);
		saveList.setAdapter(saveCheckAdapter);
		saveList.setOnItemClickListener(this);
		saveLayout.setLayoutParams(changeLayoutParams(saveCheckObjects.size(), saveLayout));
	}
	
	private void initSmsSetView(){
		smsSetLayout = (LinearLayout) findViewById(R.id.smsSetLayout);
		smsSetList = (ListView) findViewById(R.id.smsSetList);
		
		BaseSetCheckObject baseSetCheckObject = new BaseSetCheckObject();
		baseSetCheckObject.setTitle(getResources().getString(R.string.sms_set_title));
		baseSetCheckObject.setExplain(getResources().getString(R.string.sms_set_explain));
		baseSetCheckObject.setIfChcek(nhSmsMgr.getBaseShowSendBackMsg());
		baseSetCheckObject.setHeadSet(true);
		baseSetCheckObject.setHasCheck(true);
		smsSetCheckObjects.add(baseSetCheckObject);
		
		baseSetCheckObject = new BaseSetCheckObject();
		baseSetCheckObject.setTitle(getResources().getString(R.string.sms_info_title));
		baseSetCheckObject.setExplain(getResources().getString(R.string.sms_info_explain, nhSmsMgr.getBaseSignature()));
		baseSetCheckObject.setIfChcek(nhSmsMgr.getBaseSendInfoMsg());
		baseSetCheckObject.setHasCheck(true);
		baseSetCheckObject.setHeadSet(true);
		smsSetCheckObjects.add(baseSetCheckObject);
		
		smsSetCheckAdapter = new BaseSetCheckAdapter(this, smsSetCheckObjects, true);
		smsSetList.setAdapter(smsSetCheckAdapter);
		smsSetList.setOnItemClickListener(this);
		smsSetLayout.setLayoutParams(changeLayoutParams(smsSetCheckObjects.size(), smsSetLayout));
	}
	
	private void initsmsNotifView(){
		smsNotifLayout = (LinearLayout) findViewById(R.id.smsNotifLayout);
		smsNotifList = (ListView) findViewById(R.id.smsNotifList);
		
		BaseSetCheckObject baseSetCheckObject = new BaseSetCheckObject();
		baseSetCheckObject.setTitle(getResources().getString(R.string.sms_notif_title));
		baseSetCheckObject.setExplain(getResources().getString(R.string.sms_notif_explain));
		baseSetCheckObject.setIfChcek(nhSmsMgr.getBaseShowNotif());
		baseSetCheckObject.setHasCheck(true);
		baseSetCheckObject.setHeadSet(true);
		smsNotifCheckObjects.add(baseSetCheckObject);
		
		baseSetCheckObject = new BaseSetCheckObject();
		baseSetCheckObject.setTitle(getResources().getString(R.string.sms_notif_ring_title));
		baseSetCheckObject.setExplain(getResources().getString(R.string.sms_notif_ring_explain));
		baseSetCheckObject.setIfChcek(false);
		baseSetCheckObject.setHasCheck(false);
		smsNotifCheckObjects.add(baseSetCheckObject);
		
		baseSetCheckObject = new BaseSetCheckObject();
		baseSetCheckObject.setTitle(getResources().getString(R.string.sms_notif_vtong_title));
		baseSetCheckObject.setExplain(getResources().getString(R.string.sms_notif_vtong_explain));
		baseSetCheckObject.setIfChcek(nhSmsMgr.getBaseGetMsgVibrator());
		baseSetCheckObject.setHasCheck(true);
		smsNotifCheckObjects.add(baseSetCheckObject);
		
		baseSetCheckObject = new BaseSetCheckObject();
		baseSetCheckObject.setTitle(getResources().getString(R.string.sms_notif_name_title));
		baseSetCheckObject.setExplain(getResources().getString(R.string.sms_notif_name_explain));
		baseSetCheckObject.setIfChcek(nhSmsMgr.getBaseShowNewSmsName());
		baseSetCheckObject.setHasCheck(true);
		smsNotifCheckObjects.add(baseSetCheckObject);
		
		baseSetCheckObject = new BaseSetCheckObject();
		baseSetCheckObject.setTitle(getResources().getString(R.string.sms_notif_msg_title));
		baseSetCheckObject.setExplain(getResources().getString(R.string.sms_notif_msg_explain));
		baseSetCheckObject.setIfChcek(nhSmsMgr.getBaseShowNewSmsBody());
		baseSetCheckObject.setHasCheck(true);
		smsNotifCheckObjects.add(baseSetCheckObject);
		
		baseSetCheckObject = new BaseSetCheckObject();
		baseSetCheckObject.setTitle(getResources().getString(R.string.sms_notif_head_title));
		baseSetCheckObject.setExplain(getResources().getString(R.string.sms_notif_head_explain));
		baseSetCheckObject.setIfChcek(nhSmsMgr.getBaseShowNewSmsHeadImage());
		baseSetCheckObject.setHasCheck(true);
		smsNotifCheckObjects.add(baseSetCheckObject);
		
		smsNotifCheckAdapter = new BaseSetCheckAdapter(this, smsNotifCheckObjects, nhSmsMgr.getBaseShowNotif());
		smsNotifList.setAdapter(smsNotifCheckAdapter);
		smsNotifList.setOnItemClickListener(this);
		smsNotifLayout.setLayoutParams(changeLayoutParams(smsNotifCheckObjects.size(), smsNotifLayout));
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.back_btn) {
			finishActivityThing();
		} else if(v.getId() == R.id.checkbtn_cancel){
			String signature = editText.getText().toString().trim();
			if(!TextUtils.isEmpty(signature)){
				nhSmsMgr.setBaseSignature(signature);
				smsSetCheckObjects.get(1).setExplain(getResources().getString(R.string.sms_info_explain, signature));
				smsSetCheckAdapter.notifyDataSetChanged();
			} else {
				
			}
			mPopupWindow.dismiss();
		} else if(v.getId() == R.id.checkbtn_ok){
			mPopupWindow.dismiss();
		}
	}

	private void finishActivityThing() {
		nhSmsApp.setActivity(null);
		finish();
		System.gc();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			finishActivityThing();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	PopupWindow mPopupWindow = null;
	EditText editText;
	private void initPopupWindows(){
		LayoutInflater mLayoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);  
        View popunwindwow = mLayoutInflater.inflate( R.layout.popup_edit_layout, null);
        editText = (EditText) popunwindwow.findViewById(R.id.baseSignature_edit);
        if(!nhSmsMgr.getBaseSignature().equals("无"))
        	editText.setText(nhSmsMgr.getBaseSignature());
        else
        	editText.setHint(getResources().getString(R.string.signature_key_word));
        TextView title_text = (TextView) popunwindwow.findViewById(R.id.title_text);
        TextView line = (TextView) popunwindwow.findViewById(R.id.line);
        title_text.setVisibility(View.GONE);
        line.setVisibility(View.GONE);
        Button checkbtn_cancel =(Button) popunwindwow.findViewById(R.id.checkbtn_cancel);
        Button checkbtn_ok =(Button) popunwindwow.findViewById(R.id.checkbtn_ok);
        checkbtn_cancel.setOnClickListener(BaseSetActivity.this);
        checkbtn_ok.setOnClickListener(BaseSetActivity.this);
        
        mPopupWindow = new PopupWindow(popunwindwow, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());//点击窗口外消失  
        mPopupWindow.setOutsideTouchable(true);  
        mPopupWindow.setFocusable(true);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		BaseSetCheckAdapter baseSetCheckAdapter = ((BaseSetCheckAdapter)parent.getAdapter());
		BaseSetCheckObject object = baseSetCheckAdapter.getItem(position);
		if(!baseSetCheckAdapter.getCanSetOther() && !object.isHeadSet()){
			return;
		}
		
		CheckBox cb = (CheckBox)view.findViewById(R.id.widget_checkbox);
		boolean ischecked = cb.isChecked();
		object.setIfChcek(!ischecked);
		
		if(object.getTitle().equals(getResources().getString(R.string.base_set_del_title_text))){
			//删除旧消息
			saveCheckAdapter.setCanSetOther(object.isIfChcek());
			baseSetCheckAdapter.notifyDataSetChanged();
		} else if(object.getTitle().equals(getResources().getString(R.string.sms_set_title))){
			//发送报告
			nhSmsMgr.setBaseShowSendBackMsg(object.isIfChcek());
//			smsSetCheckAdapter.setCanSetOther(object.isIfChcek());
//			baseSetCheckAdapter.notifyDataSetChanged();
		} else if(object.getTitle().equals(getResources().getString(R.string.sms_notif_title))){
			//通知
			nhSmsMgr.setBaseShowNotif(object.isIfChcek());
			smsNotifCheckAdapter.setCanSetOther(object.isIfChcek());
			baseSetCheckAdapter.notifyDataSetChanged();
		} else if(object.getTitle().equals(getResources().getString(R.string.sms_notif_ring_title))){
			//选择铃声
			Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
			intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_NOTIFICATION);
			intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,"设置通知铃声");
			startActivityForResult(intent, NOTIFICATION);
		} else if(object.getTitle().equals(getResources().getString(R.string.sms_notif_vtong_title))){
			//振动
			nhSmsMgr.setBaseGetMsgVibrator(object.isIfChcek());
		} else if(object.getTitle().equals(getResources().getString(R.string.sms_notif_name_title))){
			//显示名字
			nhSmsMgr.setBaseShowNewSmsName(object.isIfChcek());
		} else if(object.getTitle().equals(getResources().getString(R.string.sms_notif_msg_title))){
			//显示内容
			nhSmsMgr.setBaseShowNewSmsBody(object.isIfChcek());
		} else if(object.getTitle().equals(getResources().getString(R.string.sms_notif_head_title))){
			//显示头像
			nhSmsMgr.setBaseShowNewSmsHeadImage(object.isIfChcek());
		} else if(object.getTitle().equals(getResources().getString(R.string.sms_info_title))){
			nhSmsMgr.setBaseSendInfoMsg(object.isIfChcek());
			
			if(object.isIfChcek()){
				if(mPopupWindow == null)
					initPopupWindows();
				
				mPopupWindow.update();
				mPopupWindow.showAsDropDown(findViewById(R.id.smsSetList));
			}
		} else if(object.getTitle().equals(getResources().getString(R.string.sms_safety_set_title))){
			if(object.isIfChcek()){
				smsSafetyCheckAdapter.setCanSetOther(true);
				smsSafetyCheckAdapter.notifyDataSetChanged();
				Intent intent = new Intent(BaseSetActivity.this, SetPasswordActivity.class);
				BaseSetActivity.this.startActivityForResult(intent, SETPASSWORD);
			} else {
				Intent intent = new Intent(BaseSetActivity.this, ConfirmPasswordActivity.class);
				intent.putExtra("inentAction", "BaseSetActivity");
				BaseSetActivity.this.startActivityForResult(intent, CONFIRMPASSWORD);
			}
		} else if(object.getTitle().equals(getResources().getString(R.string.sms_safety_set_vibor_title))){
			//触感反馈
			nhSmsMgr.setBaseCipherViewVibor(object.isIfChcek());
		} else if(object.getTitle().equals(getResources().getString(R.string.sms_safety_set_visible_title))){
			//图案可见
			nhSmsMgr.setBaseCipherViewVisible(object.isIfChcek());
		}
		
		cb.setChecked(!ischecked);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(resultCode != RESULT_OK){
			return;
		}
		
		if(CONFIRMPASSWORD == requestCode || SETPASSWORD == requestCode){
			if(TextUtils.isEmpty(nhSmsMgr.getBaseSafetyCipher())){
				smsSafetyCheckObjects.get(0).setIfChcek(false);
				smsSafetyCheckAdapter.setCanSetOther(false);
				smsSafetyCheckAdapter.notifyDataSetChanged();
			}
		}
		
		if(data == null || data.getExtras() == null){
			return;
		}
		
		Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
		if (uri != null && NOTIFICATION == requestCode) {
			RingtoneManager.setActualDefaultRingtoneUri(this,RingtoneManager.TYPE_NOTIFICATION, uri);
		}
	}

}
